# Copyright (C) 2025 Kumo inc.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

"""Tests for test fail fast protocol."""

import subprocess

from turbo import logging
from turbo.testing import _bazelize_command
from turbo.testing import turbotest
from turbo.testing import parameterized
from turbo.testing.tests import turbotest_env


@parameterized.named_parameters(
    ('use_app_run', True),
    ('no_argv', False),
)
class TestFailFastTest(parameterized.TestCase):
  """Integration tests: Runs a test binary with fail fast.

  This is done by setting the fail fast environment variable
  """

  def setUp(self):
    super().setUp()
    self._test_name = 'turbo/testing/tests/turbotest_fail_fast_test_helper'

  def _run_fail_fast(self, fail_fast, use_app_run):
    """Runs the py_test binary in a subprocess.

    Args:
      fail_fast: string, the fail fast value.
      use_app_run: bool, whether the test helper should call
          `turbotest.main(argv=)` inside `app.run`.

    Returns:
      (stdout, exit_code) tuple of (string, int).
    """
    env = turbotest_env.inherited_env()
    if fail_fast is not None:
      env['TESTBRIDGE_TEST_RUNNER_FAIL_FAST'] = fail_fast
    env['USE_APP_RUN'] = '1' if use_app_run else '0'

    proc = subprocess.Popen(
        args=[_bazelize_command.get_executable_path(self._test_name)],
        env=env,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True)
    stdout = proc.communicate()[0]

    logging.info('output: %s', stdout)
    return stdout, proc.wait()

  def test_no_fail_fast(self, use_app_run):
    out, exit_code = self._run_fail_fast(None, use_app_run)
    self.assertEqual(1, exit_code)
    self.assertIn('class A test A', out)
    self.assertIn('class A test B', out)
    self.assertIn('class A test C', out)
    self.assertIn('class A test D', out)
    self.assertIn('class A test E', out)

  def test_empty_fail_fast(self, use_app_run):
    out, exit_code = self._run_fail_fast('', use_app_run)
    self.assertEqual(1, exit_code)
    self.assertIn('class A test A', out)
    self.assertIn('class A test B', out)
    self.assertIn('class A test C', out)
    self.assertIn('class A test D', out)
    self.assertIn('class A test E', out)

  def test_fail_fast_1(self, use_app_run):
    out, exit_code = self._run_fail_fast('1', use_app_run)
    self.assertEqual(1, exit_code)
    self.assertIn('class A test A', out)
    self.assertIn('class A test B', out)
    self.assertIn('class A test C', out)
    self.assertNotIn('class A test D', out)
    self.assertNotIn('class A test E', out)

  def test_fail_fast_0(self, use_app_run):
    out, exit_code = self._run_fail_fast('0', use_app_run)
    self.assertEqual(1, exit_code)
    self.assertIn('class A test A', out)
    self.assertIn('class A test B', out)
    self.assertIn('class A test C', out)
    self.assertIn('class A test D', out)
    self.assertIn('class A test E', out)


if __name__ == '__main__':
  turbotest.main()
